package lolxxs.chenyanting.controller;


import lolxxs.chenyanting.entity.Result;
import lolxxs.chenyanting.entity.StatusCode;
import lolxxs.chenyanting.model.Log;
import lolxxs.chenyanting.service.ILogService;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @since 2021-12-30
 */
@RestController
@RequestMapping("/log")
public class LogController {

    @Autowired
    @Qualifier(value = "logServiceImpl")
    private ILogService logService;


    @PostMapping("/add")
    public Result add(HttpServletRequest request, @RequestBody Log log) {
        Claims claims = (Claims)request.getAttribute("claims");
        if (claims != null) {
            if (claims.getId() != null && (Integer)claims.get("identity") >=  1) {
                log.setUserId(Long.valueOf(claims.getId()));
//                log.setIp(getIpAddress(request));
                int row = logService.add(log);
                if (row > 0) {
                    return new Result(true, StatusCode.OK, "添加成功", row);
                } else {
                    return new Result(true, StatusCode.NOTFOUND, "添加失败");
                }
            }
        }
        return new Result(false, StatusCode.ACCESSERROR, "没有权限");
    }


    @PostMapping("/update")
    public Result update(HttpServletRequest request, @RequestBody Log log) {
        Claims claims = (Claims)request.getAttribute("claims");
        if (claims != null) {
            if (claims.getId() != null && (Integer)claims.get("identity") >=  1) {
                int row = logService.update(log);
                if (row > 0) {
                    return new Result(false, StatusCode.OK, "修改成功");
                }
            }
        }
        return new Result(true, StatusCode.UPDATEERROR, "修改失败");
    }

    @PostMapping("/query")
    public Result query(HttpServletRequest request, @RequestBody Log log) {
        Claims claims = (Claims)request.getAttribute("claims");
        if (claims != null) {
            if (claims.getId() != null && (Integer)claims.get("identity") >=  1) {
                List<Log> query = logService.query(log);
                if (query != null && query.size() > 0) {
                    return new Result(true, StatusCode.OK, "查询成功",query);
                } else {
                    return new Result(true, StatusCode.NOTFOUND, "没有找到记录");
                }
            }
        }
        return new Result(false, StatusCode.ACCESSERROR, "没有权限");
    }

    @PostMapping("/delete")
    public Result delete(HttpServletRequest request, @RequestBody Log log) {
        Claims claims = (Claims)request.getAttribute("claims");
        if (claims != null) {
            if (claims.getId() != null && (Integer)claims.get("identity") >=  1) {
                int row = logService.delete(log);
                if (row > 0) {
                    return new Result(true, StatusCode.OK, "删除成功", row);
                } else {
                    return new Result(true, StatusCode.NOTFOUND, "没有找到记录");
                }
            }
        }
        return new Result(false, StatusCode.ACCESSERROR, "没有权限");
    }


    /**
     * 获取Ip地址
     * @param request
     * @return
     */
    private static String getIpAddress(HttpServletRequest request) {
        String Xip = request.getHeader("X-Real-IP");
        String XFor = request.getHeader("X-Forwarded-For");
        if(StringUtils.isNotEmpty(XFor) && !"unKnown".equalsIgnoreCase(XFor)){
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = XFor.indexOf(",");
            if(index != -1){
                return XFor.substring(0,index);
            }else{
                return XFor;
            }
        }
        XFor = Xip;
        if(StringUtils.isNotEmpty(XFor) && !"unKnown".equalsIgnoreCase(XFor)){
            return XFor;
        }
        if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("Proxy-Client-IP");
        }
        if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("HTTP_CLIENT_IP");
        }
        if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getRemoteAddr();
        }
        return XFor;
    }
}
